﻿using System;
using System.Collections.ObjectModel;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace LFSRecord2.Misc
{
    public class DevicesInfo
    {
        public Collection<DisplayDevice> Devices { get; private set; }

        public DevicesInfo()
        {
            Devices = new Collection<DisplayDevice>();
            EnumDevices();
        }

        private void EnumDevices()
        {
            //populates Display Devices list
            Devices.Clear();
            DisplayDevice d = new DisplayDevice(0);

            int devNum = 0;
            bool result;
            do
            {
                result = NativeMethods.EnumDisplayDevices(IntPtr.Zero, devNum, ref d, 0);

                if (result)
                {
                    Devices.Add(d);
                }
                devNum++;
            } while (result);
        }

        public static Collection<DeviceMode> EnumModes(int devNum)
        {
            Collection<DeviceMode> modes = new Collection<DeviceMode>();
            string devName = GetDeviceName(devNum);
            DeviceMode devMode = new DeviceMode();
            int modeNum = 0;
            bool result = true;
            do
            {
                result = NativeMethods.EnumDisplaySettings(devName, modeNum, ref devMode);

                if (result)
                {
                    modes.Add(devMode);
                    //Debug.WriteLine("devmode : {0}", devMode.DMBitsPerPel);
                }
                modeNum++;
            } while (result);

            return modes;
        }

        //private DeviceMode GetDevmode(int devNum, int modeNum)
        //{
        //    DeviceMode devMode = new DeviceMode();
        //    string devName = GetDeviceName(devNum);
        //    EnumDisplaySettings(devName, modeNum, ref devMode);
        //    return devMode;
        //}

        private static string GetDeviceName(int devNum)
        {
            DisplayDevice d = new DisplayDevice(0);
            bool result = NativeMethods.EnumDisplayDevices(IntPtr.Zero, devNum, ref d, 0);
            return (result ? d.DeviceName.Trim() : "#error#");
        }

        //private bool MainDevice(int devNum)
        //{
        //    DisplayDevice d = new DisplayDevice(0);
        //    if (EnumDisplayDevices(IntPtr.Zero, devNum, ref d, 0))
        //    {
        //        return ((d.StateFlags & 4) != 0);
        //    }
        //    return false;
        //}
    }

    internal static class NativeMethods
    {
        [DllImport("User32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        internal static extern bool EnumDisplayDevices(IntPtr lpDevice, int iDevNum, ref DisplayDevice lpDisplayDevice, int dwFlags);

        [DllImport("User32.dll", CharSet = CharSet.Ansi, BestFitMapping = false)]
        [return: MarshalAs(UnmanagedType.Bool)]
        internal static extern bool EnumDisplaySettings(string devName, int modeNum, ref DeviceMode devMode);

        //[DllImport("user32.dll")]
        //private static extern int ChangeDisplaySettings(ref DeviceMode devMode, int flags);
    }
}
